package controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import beans.TeamMemberItemBean;
import sql.TeamMemberDao;

/**
 * Servlet implementation class GetTeamMemberData
 */
@WebServlet("/GetTeamMemberData")
public class GetTeamMemberData extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public GetTeamMemberData() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		TeamMemberDao teamMemberDao = new TeamMemberDao();
		String idAsString = request.getParameter("teamMemberId");
		String email = request.getParameter("teamMemberEmail");

		int id = 0;
		if (idAsString != null && ! idAsString.isEmpty()) {
			id = Integer.parseInt(idAsString);
		}
		try {
			TeamMemberItemBean teamMemberItemBean = null;
			
			if (id != 0 && email == null) {	// get team member data by id
				teamMemberItemBean = teamMemberDao.getTeamMemberInfo(id);
			}
			else {	// get team member data by email (for validation checks)
				teamMemberItemBean = teamMemberDao.getTeamMemberInfo(email);
			}
			PrintWriter out = response.getWriter();
			out.write(teamMemberItemBean.getJson());
		} catch (ClassNotFoundException | SQLException e) {
			e.printStackTrace();
		}
	}

}
